package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.RepaymentListByStageAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.RepaymentListByStageBean;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 某一期的还款列表
 */
public class RepaymentListByStageActivity extends BaseActivity implements HttpCallback {

    @BindView(R.id.RepaymentListByStage_hint)
    TextView RepaymentListByStageHint;
    @BindView(R.id.RepaymentListByStage_RecyclerView)
    RecyclerView RepaymentListByStageRecyclerView;
    private RepaymentListByStageAdapter adapter;
    private List<RepaymentListByStageBean> list = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.activity_repayment_list_by_stage;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("还款列表");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        RepaymentListByStageRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new RepaymentListByStageAdapter(mContext, R.layout.item_repaymentbystage, list);
        RepaymentListByStageRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.REPAY_LIST, mContext,
                new String[]{"accessid", "loan_no"},
                new String[]{SPUtils.getAccessid(mContext), getIntent().getExtras().getString("loan_no")}), mContext, HttpURL.REPAY_LIST, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        list.addAll(JsonUtils.getInstance().getList(content, "repay_list", RepaymentListByStageBean.class));
        adapter.notifyDataSetChanged();
        String unrepay = "剩余待还  " + JsonUtils.getInstance().getJsonObject(content, "unrepay") + "元";
        RepaymentListByStageHint.setText(BaseUtils.setTextColor(mContext, unrepay, 4, unrepay.length(), R.color.colorRed));
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }
    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
